﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Chenxin.Lib.Hr.BLL
{
    public class OrganizationManage
    {
        public OrganizationManage()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一个关系
        /// </summary>
        public void AddSubTree(Guid gdParentId ,Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo model)
        {
            Chenxin.Lib.Hr.BLL.Hr_DepartmentInfo bllDepart = new Hr_DepartmentInfo();
            Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation entDepartTree = new Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation();

            entDepartTree.RecordId = Guid.NewGuid();
            entDepartTree.ParentId = gdParentId;
            entDepartTree.ChildId = model.DepartId;

            bllDepart.Add(model);
            Chenxin.Lib.Hr.DAL.Hr_Struct_DepartTreeRelation.Add(entDepartTree);

            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            //删除组织结构缓存
            string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_STRUCT_TREE, gdParentId);
            cs.Delete(strCacheKey);
            strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_STRUCT_TREE, model.CompanyId+".4000");
            cs.Delete(strCacheKey);
            strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_STRUCT_TREE, model.CompanyId + ".5000");
            cs.Delete(strCacheKey);
            
            //删除部门信息缓存
            strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, model.DepartId);
            cs.Delete(strCacheKey);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> GetListByParentId(Guid gdParentId)
        {
            //Chenxin.Lib.Hr.BLL.Hr_Struct_DepartTreeRelation bllDepartTree = new Hr_Struct_DepartTreeRelation();
            //return bllDepartTree.GetModelList(" ParentId = '" + gdParentId.ToString() + "'");
            string sql = string.Format("select b.* from Hr_Struct_DepartTreeRelation left join Hr_DepartmentInfo as b on Hr_Struct_DepartTreeRelation.ChildId=b.DepartId where ParentId='{0}'", gdParentId.ToString());
            return Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetListBySql(sql);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> GetListBySql(string strwhere)
        {
            //Chenxin.Lib.Hr.BLL.Hr_Struct_DepartTreeRelation bllDepartTree = new Hr_Struct_DepartTreeRelation();
            //return bllDepartTree.GetModelList(" ParentId = '" + gdParentId.ToString() + "'");
            string sql = string.Format("select b.* from Hr_Struct_DepartTreeRelation left join Hr_DepartmentInfo as b on Hr_Struct_DepartTreeRelation.ChildId=b.DepartId where {0} order by b.departname", strwhere);
            return Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetListBySql(sql);
        }

        /// <summary>
        /// 老方法，后期删除
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation> Old_GetListByParentId(Guid gdParentId)
        {
            Chenxin.Lib.Hr.BLL.Hr_Struct_DepartTreeRelation bllDepartTree = new Hr_Struct_DepartTreeRelation();
            return bllDepartTree.GetModelList(" ParentId = '" + gdParentId.ToString() + "'");
        }

        //删除树节点
        public void DeleteLeaf(Guid gdDepartId)
        {
            Chenxin.Lib.Hr.BLL.Hr_DepartmentInfo bllDepart = new Hr_DepartmentInfo();
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            string strCacheKey = "";

            Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo dep = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(gdDepartId);
            bllDepart.Delete(gdDepartId);
            string strSql = string.Format("ChildId='{0}'", gdDepartId.ToString());
            List < Chenxin.Lib.Hr.Entity.Hr_Struct_DepartTreeRelation > list = Chenxin.Lib.Hr.DAL.Hr_Struct_DepartTreeRelation.GetListArray(strSql);
            if (list.Count > 0)
            {
                Guid parentId = list[0].ParentId;
                //删除组织结构缓存
                strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_STRUCT_TREE, parentId);
                cs.Delete(strCacheKey);
            }
            strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_STRUCT_TREE, dep.CompanyId + ".4000");
            cs.Delete(strCacheKey);
            strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_STRUCT_TREE, dep.CompanyId + ".5000");
            cs.Delete(strCacheKey);

            Chenxin.Lib.Hr.DAL.Hr_Struct_DepartTreeRelation.DeleteByChild(gdDepartId);
            string strsql = string.Format("delete Struct_UserDepartRelation where DepartId='{0}';", gdDepartId.ToString());
            strsql += string.Format("delete HR_Rel_Purview where DepartId='{0}';", gdDepartId.ToString());
            Chenxin.Lib.Hr.DAL.Struct_UserDepartRelation.ExecuteSQL(strsql);           

            //删除部门信息缓存
            strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, gdDepartId);
            cs.Delete(strCacheKey);
            //删除部门设定缓存
            strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPART_SETTINGS, gdDepartId);
            cs.Delete(strCacheKey);
        }


        public Boolean hasChildDepart(Guid gdParentDepart)
        {
           return  Chenxin.Lib.Hr.DAL.Hr_Struct_DepartTreeRelation.HasChildDepart(gdParentDepart);
        }

        //得到用户角色
        public List<Chenxin.Lib.Hr.Entity.Base_RoleUserRelationInfo> GetRoleListByUserId(string strUserId)
        {
            string strWhere;
            strWhere = "UserId = '" + strUserId + "'";
            return Chenxin.Lib.Hr.DAL.Base_RoleUserRelationInfo.GetListArray(strWhere);
        }

        public void DeleteRolesByUserId(Guid UserId)
        {
            Chenxin.Lib.Hr.DAL.Base_RoleUserRelationInfo.DeleteByUserId(UserId);
        }

        public void AddRole(Chenxin.Lib.Hr.Entity.Base_RoleUserRelationInfo model)
        {
            Chenxin.Lib.Hr.DAL.Base_RoleUserRelationInfo.Add(model);
        }


        /// <summary>
        /// 获取树节点
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DataSet GetTreeList(string strWhere, int type)
        {
            if (type == 500 || type == 600)
            {
                return Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetUserTreeList(strWhere);
            }
            else//查询部门组织
            {
                return Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetTreeList(strWhere);
            }
        }

        #endregion
    }
}
